import { baseURL } from './config.js'

export const request = (opts) => {
	let data = opts.data || {}
	let params = {}
	
	for(key in data) {
		if (data[key] === 0) {
			params[key] = data[key]
		}
		else {
			if (data[key]) {
				params[key] = data[key]
			}
		}
	}
	
	let headers = {
		Accept: 'application/vnd.edusoho.v2+json',
		'Content-Type': 'application/x-www-form-urlencoded'
	}
	
	Object.assign(headers, opts.headers)
	
	let defaultOpts = {
		header: headers,
		url: baseURL + opts.url,
		method: opts.method || 'GET',
		dataType: opts.dataType || 'json',
		data: params
	}
	
	
	if (opts.header && !opts.header.Authorization) return
	
	let promise = new Promise((resolve, reject) => {
		uni.request(defaultOpts)
			.then((response) => {
				resolve(response[1])
			})
			.catch((error) => {
				reject(error)
			})
	})
	
	return promise
}
